package config.java;

import java.io.File;
import java.io.FileInputStream;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

public class PropertyConfig extends ParseConfig {

	@Override
	protected void parse(String alias, String file) {
		ConfigLog.debugLog("Property Config load file:" + file);
		File f = null;
		FileInputStream fi = null;
		ConfigValue value = ConfigValue.getInstance();
		ConfigBin b = value.getConfBin(alias);
		if (b == null)
			b = new ConfigBin();
		if (b != null) {
			b.setAlias(alias);
			b.setFile(file);
			b.setLoadTime(System.currentTimeMillis());
		}
		try {
			Properties p = new Properties();
			f = new File(file);
			fi = new FileInputStream(f);
			p.load(fi);
			Iterator itr = p.entrySet().iterator();
			Hashtable<String, String> pv = new Hashtable<String, String>();
			while (itr.hasNext()) {
				Map.Entry e = (Map.Entry) itr.next();
				pv.put((String) e.getKey(), (String) e.getValue());
			}

			if (pv.size() > 0)
				value.setConfigValue(alias, pv);
			ConfigLog.debugLog("Property Config file:" + file
					+ " load success!");
			if (b != null)
				b.setStatus(1);
		} catch (Exception ex) {
			ConfigLog
					.errorLog("Property Config file:" + file + " load failed!");
			ConfigLog.errorLog(ex);
			if (b != null) {
				b.setStatus(-1);
				b.setError(ex.toString());
			}
		} finally {
			if (fi != null) {
				try {
					fi.close();
					fi = null;
				} catch (Exception e) {
				}
			}
			if (f != null) {
				f = null;
			}
			value.setConf(b);
		}
	}

}
